/*****************************************************************************
 * 
 * Copyright [2013] [Mervin.Wong]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 *       
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *  
 *****************************************************************************/
package util;

import java.util.ArrayList;
import java.util.Iterator;

/**
 *@name PairList.java
 *@description 
 *@author 王进法<Mervin.Wong>
 *@version 
 *@Date 2013-1-28下午6:50:49
 */
/*********************************************************************************
 *
 *
 **********************************************************************************/

public class PairList<L, R> {
	private ArrayList<L> valuesL = new ArrayList<L>();
	private ArrayList<R> valuesR = new ArrayList<R>();
	private int length = 0;
	
	public PairList(){
	}
	
	/**
	 * @name clear
	 * @description 清空list
	 */
	public void clear(){
		this.valuesR.clear();
		this.valuesL.clear();
	}
	/**
	 * @name contains
	 * @description 判断是否含有valueL valueR数值对
	 * @param valueL
	 * @param valueR
	 * @return
	 */
	public boolean contains(L valueL, R valueR){
		if(this.getIndex(valueL, valueR) != -1){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * @name isEmpty
	 * @description 判断是否为空
	 * @return
	 */
	public boolean isEmpty(){
		if(this.length == 0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * @name getL
	 * @description 获取索引i的左值
	 * @param i
	 * @return
	 */
	public L getL(int i){
		return this.valuesL.get(i);
	}
	/**
	 * @name getR
	 * @description 取索引i的右值
	 * @param i
	 * @return
	 */
	public R getR(int i){
		return this.valuesR.get(i);
	}
	
	/**
	 * @name remove
	 * @description 移除valueL valueR数值对
	 * @param valueL
	 * @param valueR
	 */
	public void remove(L valueL, R valueR){
		int i = 0;
		if((i = this.getIndex(valueL, valueR)) != -1 ){
			this.valuesL.remove(i);
			this.valuesR.remove(i);
		}
	}
	/**
	 * @name add
	 * @description 添加valueL valueR数值对
	 * @param valueL
	 * @param valueR
	 */
	public void add(L valueL, R valueR){
		this.valuesL.add(valueL);
		this.valuesR.add(valueR);
		this.length++;
	}
	
	/**
	 * @name size
	 * @description 
	 * @return
	 */
	public int size(){
		return this.length;
	}

	
	
	
	/**
	 * @name getIndex
	 * @description 获取valueL,valueR 所在的索引号
	 * @param valueL
	 * @param valueR
	 * @return
	 */
	private int getIndex(L valueL, R valueR){
		int i = 0, flag = -1;
		for (Iterator<L> iterator = this.valuesL.iterator(); iterator.hasNext();) {
			if(iterator.next().equals(valueL) && this.valuesR.get(i).equals(valueR)){
				flag = i;
				break;
			}
			i++;
		}
		return flag;
	}
	
	public static void  main(String[] args) {
	}
}
